package com.icangya.DAO.impl;

import com.icangya.DAO.IFbMissionRecordDAO;
import com.icangya.domain.FbMissionRecord;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.List;


@Repository
public class FbMissionRecordDAOImpl extends  BaseDAO<FbMissionRecord> implements IFbMissionRecordDAO {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public List<FbMissionRecord> getFbMissionRecord(Timestamp now) {
        String hql="FROM FbMissionRecord mr where to_days(mr.createTime)= to_days(?)";
        return (List<FbMissionRecord>) getHibernateTemplate().find(hql,now);

    }

    @Override
    public FbMissionRecord getMissionRecord(Timestamp now, long missionId) {

        String hql="FROM FbMissionRecord mr where to_days(mr.createTime)= to_days(?) and mr.missionId=?";
        List<FbMissionRecord> list= (List<FbMissionRecord>) getHibernateTemplate().find(hql,now,missionId);
        if(list.size()>0)
            return list.get(0);
        return null;
    }

    @Override
    public int getMissionRecordCount(Timestamp now, String openId) {

        String hql="select count(*) from fb_mission_record mr where TO_DAYS(mr.create_time) =TO_DAYS(?) and " +
                "mr.id not in (SELECT um.mission_id from  fb_user_mission um where  TO_DAYS(um.create_time) =TO_DAYS(?) and  um.openid=?)  ";
        Session session=  getHibernateTemplate().getSessionFactory().getCurrentSession();
        Query query=  session.createSQLQuery(hql);
        query.setParameter(0,now);
        query.setParameter(1,now);
        query.setParameter(2,openId);
       List list=  query.list();
        if(list.size()>0) {
            int count=Integer.parseInt(list.get(0).toString());
            logger.info(count+"===============count");
            return count ;
        }
        return 0;
    }
}
